package com.kgc.app.controller;

import com.github.pagehelper.PageInfo;
import com.kgc.app.pojo.EbookEntry;
import com.kgc.app.service.EbookEntryService;
import org.apache.taglibs.standard.extra.spath.Step;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * 要求：
 *
 * @Date 2021/9/8
 */
@Controller
@RequestMapping("/bill")
public class EbookEntryController {
    @Autowired
    private EbookEntryService ebookEntryService;

    @RequestMapping("/list")
    public String list(@RequestParam(required = false,defaultValue = "0") Integer id,
                       @RequestParam(required = false,defaultValue = "1") Integer pageNo, Model model){
        PageInfo<EbookEntry> pageInfo = ebookEntryService.selectBookByCondition(id, pageNo, 5);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("id",id);
        return "billlist";
    }

    @RequestMapping("/add")
    public String addBook(EbookEntry ebookEntry){
        int i = ebookEntryService.addBook(ebookEntry);
        if (i == 0){
            return "redirect:/jsp/billadd.jsp";
        }
        return "redirect:/bill/list";
    }

    @RequestMapping("/delete")
    public String delete(Integer id){
        int i = ebookEntryService.delete(id);
        return "redirect:/bill/list";
    }

    @RequestMapping("/view")
    public String view(Integer id,Model model){
        EbookEntry ebookEntry = ebookEntryService.selectBookById(id);
        model.addAttribute("ebookEntry",ebookEntry);
        return "billview";
    }
    @RequestMapping("/toUpdate")
    public String toUpdate(Integer id,Model model){
        EbookEntry ebookEntry = ebookEntryService.selectBookById(id);
        model.addAttribute("ebookEntry",ebookEntry);
        return "billmodify";
    }

    @RequestMapping("/update")
    public String update(EbookEntry ebookEntry){
        ebookEntry.setCreatedate(new Date());
        int row = ebookEntryService.update(ebookEntry);
        if (row == 0){
            return "redirect:/bill/toUpdate?id="+ebookEntry.getId();
        }
        return "redirect:/bill/list";
    }
}
